package com.lzf.yh.userService.impl;

import com.lzf.yh.commons.JsonBean;
import com.lzf.yh.commons.MD5Util;
import com.lzf.yh.dao.UserMapper;
import com.lzf.yh.dao.UsergetgoodsMapper;
import com.lzf.yh.entity.User;
import com.lzf.yh.entity.Usergetgoods;
import com.lzf.yh.userService.UsergetgoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Auther: Wyz
 * @Date: 2021/4/16 17:03
 * @Description:
 */
@Service
public class UsergetgoodsServiceImpl implements UsergetgoodsService {

    @Autowired
    private UsergetgoodsMapper usergetgoodsMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public JsonBean selectOneUsergetgoods(Integer userid) {
        JsonBean jsonBean = new JsonBean(-1, "无数据", null);
        Usergetgoods usergetgoods = usergetgoodsMapper.selectByPrimaryKey(userid);
        if (usergetgoods!=null&&usergetgoods.getUserid()!=null){
            jsonBean = new JsonBean(0, "", usergetgoods);
        }
        return jsonBean;
    }

    @Override
    public JsonBean selectOneUsergetgoodsByUserId(Integer userid) {
        return new JsonBean(0,"",usergetgoodsMapper.selectOneByUserId(userid));
    }

    @Override
    public JsonBean updateUserAllNews(User user, Usergetgoods userGetGoods) {
        JsonBean jsonBean = new JsonBean(-1, "修改失败", null);
        userGetGoods.setId(null);
        userGetGoods.setUserid(user.getId());
        userGetGoods.setUserPhone(user.getPhone());
        String encrypt = MD5Util.encrypt(user.getPwd() + user.getSalt());
        user.setPwd(encrypt);
        Usergetgoods usergetgoods = usergetgoodsMapper.selectByPrimaryKey(user.getId());
        int i =0;
        if (usergetgoods==null){
            i = usergetgoodsMapper.insertSelective(userGetGoods);
        }else {
            i = usergetgoodsMapper.updateByPrimaryKey(userGetGoods);
        }

        int i1 = userMapper.updateByPrimaryKey(user);
        if (i>0&&i1>0){
            jsonBean = new JsonBean(0,"修改成功",null);
        }
        return jsonBean;
    }

    /**
     * 充值的方法
     * @param userGetGoods
     * @return
     */
    @Override
    public JsonBean updataYue(Usergetgoods userGetGoods) {
        JsonBean jsonBean = new JsonBean(-1, "充值失败，请重试", null);
        Usergetgoods usergetgoods = usergetgoodsMapper.selectByPrimaryKey(userGetGoods.getUserid());
        if (usergetgoods==null){
           return  new JsonBean(-2, "请先设置您的基本信息", null);
        }
        userGetGoods.setBalance(userGetGoods.getBalance()+usergetgoods.getBalance());
        int i = usergetgoodsMapper.updateBalanceByUserid(userGetGoods);
        if (i>0){
            jsonBean = new JsonBean(0, "充值成功", null);
        }
        return jsonBean;
    }
}
